<template>
  <div>
    <div class="menus">
      <el-button @click="goAdd" class="btn" type="primary">添加</el-button>
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="id" label="角色编号" sortable width="180">
        </el-table-column>
        <el-table-column prop="rolename" label="角色名称" sortable width="180">
        </el-table-column>
        <el-table-column width="180" label="状态">
          <template #default="{ row: { status } }">
            <el-tag type="success" v-show="status === 1">正常</el-tag>
            <el-tag type="danger" v-show="status === 2">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row: { id } }">
            <el-button
              type="primary"
              size="mini"
              @click="$router.push({ path: '/setRole', query: { id } })"
              >编辑</el-button
            >
            <el-button @click="del(id)" type="danger" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getRoleListApi, delRoleApi } from "@/api/roles";
export default {
  data() {
    return {
      tableData: [],
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      let res = await getRoleListApi();
      this.tableData = res.list;
      console.log(res);
    },
    goAdd() {
      this.$router.push("/addRole");
    },
    async del(id) {
      /* alert(res.msg); */
      this.$confirm("删除后数据无法恢复，是否确认删除", "警告信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "删除",
        cancelButtonText: "取消",
      })
        .then(async () => {
          let res = await delRoleApi(id);
          if (res.code == 200) {
            this.$message({
              type: "info",
              message: "删除成功",
            });
            this.getData();
          } else {
            alert(res.msg);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "删除事件取消" 
          });
        });
    },
  },
};
</script>
<style lang="less" scoped>
.menus {
  .btn {
    margin-bottom: 20px;
  }
}
</style>
